﻿using DatabaseCore.Dao;
using DatabaseCore.Dto;
using NawaData.Dao;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DatabaseCore
{
    public class TransactionDao
    {
        public bool Insert(tbl_Transaction entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                ctx.tbl_Transaction.Add(entity);
                return ctx.SaveChanges() > 0;
            }
        }

        public bool InsertCollection(List<tbl_Transaction> entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                ctx.tbl_Transaction.AddRange(entity);
                return ctx.SaveChanges() > 0;
            }
        }

        public List<tbl_Transaction> GetList()
        {
            using (var ctx = new ExportDBEntities())
            {
                var trans = (from x in ctx.tbl_Transaction
                             select x);

                return trans.ToList();
            }
        }

        public bool CheckisAlreadyMoveToSapOrderNoAndMatNo(string OrderNo, string MatNo)
        {
            var result = false;
            using (var ctx = new ExportDBEntities())
            {
                var trans = (from x in ctx.tbl_Transaction
                             where x.OrderNo == OrderNo && x.MaterialNo == MatNo
                             select x).FirstOrDefault();
                if (trans != null)
                {
                    if (trans.IsMoveCompleted ?? false)
                    {
                        result = true;
                    }
                }
            }

            return result;
        }

        public List<tbl_Transaction> GetTransectionByOrderNoAndMatNo(string OrderNo, string MatNo)
        {
            using (var ctx = new ExportDBEntities())
            {
                var trans = (from x in ctx.tbl_Transaction
                             where x.OrderNo == OrderNo && x.MaterialNo == MatNo
                             select x).ToList();
                var movementProvider = new ProductMovementDaocs();
                List<decimal> PalletIds = new List<decimal>();
                foreach (var t in trans)
	            {
		            if (t.PalletDetailId != null){
                        PalletIds.Add(t.PalletDetailId ?? 0);
                    }
	            }


                var allmove = movementProvider.getMoveTypeByPalletIds(PalletIds);
                foreach (var t in trans)
                {
                    foreach (var move in allmove)
                    {
                        if (t.PalletDetailId == move.PalletId)
                        {
                            t.MMoveSubCate = move.MSubCate;
                            t.Mreverse = move.MReverse;
                        }
                    }
                }

                return trans.ToList();
            }
        }

        public List<TransectionOrder> GetTransectionDetails(DateTime DateFrom, DateTime DateTo,
            string PlantSrc,
            string PlantTo,
            string OrderId,
            string ProductId,
            string PoId,
            string MoveType,
            int Status,
            string IssueBy
            )
        {
            using (var ctx = new ExportDBEntities())
            {
                var trans = (from x in ctx.tbl_Transaction
                             join o in ctx.tbl_Order on x.OrderNo equals o.OrderNo
                             join p in ctx.tbl_PlantDomExport on x.Plant equals p.PlantDom 
                             select new TransectionOrder() { Id = x.Id,
                              IsMoveCompleted = x.IsMoveCompleted,
                              MaterialNo = x.MaterialNo,
                              OrderNo = x.OrderNo,
                              PalletDetailId = x.PalletDetailId,
                              Plant = x.Plant,
                               Qty =  x.Qty,
                              Result = x.Result,
                              StockNo = x.StockNo,
                              UpdateBy = x.UpdateBy,
                              UpdateDate = x.UpdateDate,
                              RefOrderNo = o.RefOrderNo,
                              PlantExport = p.PlantExport,
                              Status = o.Status
                              });

                if (DateFrom != DateTime.MinValue)
                {
                    trans = trans.Where(t => t.UpdateDate >= DateFrom);
                }

                if (DateTo != DateTime.MinValue)
                {
                    trans = trans.Where(t => t.UpdateDate <= DateTo);
                }

                if (!string.IsNullOrEmpty(PlantSrc))
                {
                    trans = trans.Where(t => t.Plant.Contains(PlantSrc));
                }

                if (!string.IsNullOrEmpty(PlantTo))
                {
                    trans = trans.Where(t => t.PlantExport.Contains(PlantTo));
                }

                if (!string.IsNullOrEmpty(OrderId))
                {
                    trans = trans.Where(t => t.OrderNo.Contains(OrderId));
                }

                if (!string.IsNullOrEmpty(ProductId))
                {
                    trans = trans.Where(t => t.MaterialNo.Contains(ProductId));
                }

                if (!string.IsNullOrEmpty(PoId))
                {
                    trans = trans.Where(t => t.RefOrderNo.Contains(PoId));
                }



                if (!string.IsNullOrEmpty(MoveType))
                {
                    trans = trans.Where(t => t.UpdateBy == MoveType);
                }

                if (Status != -1)
                {
                    trans = trans.Where(t => t.Status == Status);
                }

                if (!string.IsNullOrEmpty(IssueBy))
                {
                    trans = trans.Where(t => t.UpdateBy == IssueBy);
                }


                trans = trans.OrderByDescending(t => t.UpdateDate);
                var alltrans = trans.ToList();
                var allMatIds = trans.Select(t => t.MaterialNo).Distinct().ToArray();
                var productDao = new ProductsDao();
                var productNames = productDao.getProductNames(allMatIds);
                foreach (var d in alltrans)
                {
                    foreach (var name in productNames)
                    {
                        if (d.MaterialNo == name.mmatno)
                        {
                            d.ProductName = name.mmateng;
                        }
                    }
                }



                return alltrans;
            }
        }
    }
}